package com.lansi.map.controller;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.lansi.bean.ResponseResult;
import com.lansi.map.bean.Mapinfo;
import com.lansi.map.service.IMapinfoService;


@Controller
public class MapinfoController {

	@Resource
	private IMapinfoService mapinfoService;
	
	//添加地图信息
	@RequestMapping("/insertMapinfo.do")
	@ResponseBody
	public ResponseResult<Void> checkInsertMapinfo(@RequestBody Map<String,String> map1) throws JsonParseException, JsonMappingException, IOException {
		
		ObjectMapper mapper = new ObjectMapper();    
		@SuppressWarnings("unchecked")
		Map<String,Object> map = mapper.readValue(map1.get("params"), Map.class); 
		
		Object mapid = map.get("mapid");
		Object sn = map.get("sn");
		Object building = map.get("building");
		Object ground = map.get("ground");
		Object width = map.get("width");
		Object height = map.get("height");
		Object memo = map.get("memo");
		
		ResponseResult<Void> rr = new  ResponseResult<Void>();
		try {
				Mapinfo mapinfo = new Mapinfo();
				if(mapid != null) mapinfo.setMapid(mapid.toString());
				if(sn != null) mapinfo.setSn(sn.toString());
				if(width == null || "".equals(width.toString().trim())) {
					mapinfo.setBuilding("1");
				}
				if(building != null) mapinfo.setBuilding(building.toString());
				if(width == null || "".equals(width.toString().trim())) {
					mapinfo.setGround("1");
				}
				if(ground != null) mapinfo.setGround(ground.toString());
				if(width == null || "".equals(width.toString().trim())) {
					mapinfo.setWidth((Integer) 0);
				}else {
					mapinfo.setWidth(Integer.parseInt(width.toString()));
				}
				if(height == null || "".equals(height.toString().trim())) {
					mapinfo.setHeight((Integer) 0);
				}else {
					mapinfo.setHeight(Integer.parseInt(height.toString()));
				}
				if(memo != null) mapinfo.setMemo(memo.toString());
				mapinfoService.getInsertMapinfo(mapinfo);
				
				rr.setState(1);
				rr.setMessage("地图信息添加成功！");
			
			
		} catch (RuntimeException e) {
			
			rr.setState(-1);
			rr.setMessage(e.getMessage());
		}
		
		return rr;
		
	}
	
	//删除地图信息
	@RequestMapping("/deleteByMapid.do")
	@ResponseBody
	public ResponseResult<Void> checkDeleteByMapid(@RequestBody Map<String,String> map
			){
		String mapid = map.get("mapid");
		mapinfoService.delMapinfoByMapid(mapid);
		
		ResponseResult<Void> rr = new ResponseResult<Void>();
		
		rr.setState(1);
		rr.setMessage("信息删除成功！");
		
		return rr;
	}
	//修改地图信息
	@SuppressWarnings("null")
	@RequestMapping("/updateMapinfo.do")
	@ResponseBody
	public ResponseResult<Void> checkUpdateMapinfo(@RequestBody Map<String,String> map1) throws JsonParseException, JsonMappingException, IOException{
		
		ObjectMapper mapper = new ObjectMapper();    
		@SuppressWarnings("unchecked")
		Map<String,Object> map = mapper.readValue(map1.get("params"), Map.class); 
		
		Object id = map.get("id");
		Object mapid = map.get("mapid");
		Object sn = map.get("sn").toString();
		Object building = map.get("building");
		Object ground = map.get("ground");
		Object width = map.get("width");
		Object height = map.get("height");
		Object memo = map.get("memo");
		ResponseResult<Void> rr = new ResponseResult<Void>();
		try {
			Mapinfo mapinfo = new Mapinfo();
			if(id != null || !"".equals(id.toString().trim())) {
				mapinfo.setId(Integer.parseInt(id.toString()));
			}
			if(mapid != null) mapinfo.setMapid(mapid.toString());
			if(sn != null) mapinfo.setSn(sn.toString());
			if(building != null) mapinfo.setBuilding(building.toString());
			if(ground != null) mapinfo.setGround(ground.toString());
			if(width == null || "".equals(width.toString().trim())) {
				mapinfo.setWidth((Integer) 0);
			}else {
				mapinfo.setWidth(Integer.parseInt(width.toString()));
			}
			if(height == null || "".equals(height.toString().trim())) {
				mapinfo.setHeight((Integer) 0);
			}else {
				mapinfo.setHeight(Integer.parseInt(height.toString()));
			}
			if(memo != null) mapinfo.setMemo(memo.toString());
			
			mapinfoService.updateMapinfoByMapid(mapinfo);
			rr.setState(1);
			rr.setMessage("信息修改成功！");
		
		} catch (RuntimeException e) {
			
			rr.setState(-1);
			rr.setMessage("信息修改失败！");
			System.out.println(e.getMessage());
		}
		
		return rr;		
	}
	
	
	//获取公司编号sn和名字name
	@RequestMapping("/getCompanySnName.do")
	@ResponseBody
	public ResponseResult<Map<String, Object>> getCompanySnNameByUsername(
			//HttpServletRequest request,
			 String username
			){
		
		ResponseResult<Map<String, Object>> rr = new ResponseResult<Map<String, Object>>();
		//User user=(User)request.getAttribute("user");
		//String prio = user.getPrio();
		//String sn = user.getSn();
		String prio = "2";//------------------------------------------------------------------------------------------------------------
		
		try {
			Map<String,Object> map = mapinfoService.getCompanySnName(username,prio);
			rr.setState(1);
			rr.setMessage("获取公司编号和名字成功！");
			rr.setData(map);
		} catch (RuntimeException e) {
			rr.setState(-1);
			rr.setMessage("获取公司编号和名字失败！");
		}
		return rr;
	}
	
	//分页查询地图信息根据用户名分权限
		@RequestMapping("/getMapinfoByUsername.do")
		@ResponseBody
		public ResponseResult<Map<String, Object>> getMapinfoByUsername(
				String username,String page//,HttpServletRequest request
				){
			ResponseResult<Map<String, Object>> rr = new ResponseResult<Map<String, Object>>();
			
//			User user = (User)request.getAttribute("user");
//			String prio = user.getPrio();
//			String sn = user.getSn();
			String sn = "2111";
			String prio = "2";//-----------------------------------------------------------------------------------------------------
			
			Integer offset = null;
			Integer count = 10;
			if(page == null) {
				offset = 0;
			}else {
				offset = (Integer.parseInt(page)-1)*10;
			}
			Map<String, Object> maputil = new HashMap<String, Object>();
			try {
				if("1".equals(prio)) {
					Map<String, Object> map = mapinfoService.getMapinfo(offset, count);
					rr.setState(1);
					rr.setMessage("查询信息成功！");
					rr.setData(map);
				}else {
					List<Mapinfo> list = mapinfoService.getMapinfoBySn(sn, offset, count);
					Integer size = mapinfoService.getSizeBySnName(sn, null);
					maputil.put("list", list);
					maputil.put("size", size);
					rr.setState(1);
					rr.setMessage("查询信息成功！");
					rr.setData(maputil);
				}
			} catch (Exception e) {
				rr.setState(1);
				rr.setMessage("查询信息失败！原因："+e.getMessage());
			}
			return rr;
	}
		
		//查询所有地图信息根据用户名分权限
				@RequestMapping("/getMapinfoMapidByUsername.do")
				@ResponseBody
				public ResponseResult<Map<String, Object>> getMapinfoMapidByUsername(
						String username,String page//,HttpServletRequest request
						){
					ResponseResult<Map<String, Object>> rr = new ResponseResult<Map<String, Object>>();
					
//					User user = (User)request.getAttribute("user");
//					String prio = user.getPrio();
//					String sn = user.getSn();
					String sn = "2111";
					String prio = "2";//-----------------------------------------------------------------------------------------------------
					
					Integer offset = null;
					Integer count = 10;
					Map<String, Object> maputil = new HashMap<String, Object>();
					try {
						if("1".equals(prio)) {
							Map<String, Object> map = mapinfoService.getMapinfo(offset, count);
							rr.setState(1);
							rr.setMessage("查询信息成功！");
							rr.setData(map);
						}else {
							List<Mapinfo> list = mapinfoService.getMapinfoBySn(sn, offset, count);
							Integer size = mapinfoService.getSizeBySnName(sn, null);
							maputil.put("list", list);
							maputil.put("size", size);
							rr.setState(1);
							rr.setMessage("查询信息成功！");
							rr.setData(maputil);
						}
					} catch (Exception e) {
						rr.setState(1);
						rr.setMessage("查询信息失败！原因："+e.getMessage());
					}
					return rr;
			}
		
		//根据username\sn和mapid查询地图信息
		@RequestMapping("/getBySnMapid.do")
		@ResponseBody
		public ResponseResult<Map<String, Object>> getMapinfoSnMapid(String username,String sn_mapid){
			
			ResponseResult<Map<String, Object>> rr = new ResponseResult<Map<String, Object>>();
			Map<String, Object> map = new HashMap<String, Object>();
			List<Mapinfo> listutil = new ArrayList<Mapinfo>();
			Integer size = null;
			try {
				if(sn_mapid == null || "".equals(sn_mapid)) {
					Map<String, Object> mapall = mapinfoService.getMapinfo(null, null);
					rr.setState(1);
					rr.setMessage("所有地图信息！");
					rr.setData(mapall);
				}else {
					Mapinfo mapinfo = mapinfoService.getMapinfoByMapid(sn_mapid);
					if(mapinfo == null) {
						List<Mapinfo> list = mapinfoService.getMapinfoByUsernameSn(sn_mapid);
						size = mapinfoService.getSizeBySnName(sn_mapid, null);
						map.put("list", list);
						map.put("size", size);
						rr.setState(1);
						rr.setMessage("根据公司编号查询成功！");
						rr.setData(map);
					}else {
						size = mapinfoService.getSizeBySnName(null, sn_mapid);
						listutil.add(mapinfo);
						map.put("list", listutil);
						map.put("size", size);
						rr.setState(1);
						rr.setMessage("根据地图名称查询成功！");
						rr.setData(map);
					}
				}
			} catch (Exception e) {
				rr.setState(-1);
				rr.setMessage("查询地图信息失败！原因："+e.getMessage());
			}
			
			return rr;
		}
		
}
